<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-button type="text" @click="headleShow"><span>一级商品分类列表</span></el-button>
        <i class="el-icon-right" v-show='show'>{{name}}</i>
        <el-button
          type="primary"
          icon="el-icon-plus"
          style="float: right; padding: 10px 10px"
          @click="open"
          >添加分类</el-button
        >
      </div>
      <template>
        <el-table
          stripe
          :data="list"
          v-loading="loading"
          border
          style="width: 100%"
        >
          <el-table-column prop="name" label="商品分类" width="800">
          </el-table-column>
          <el-table-column prop="_id" label="操作选项">
            <template slot-scope="scope">
              <el-button
                size="mini"
                @click="handelupdateCategory(scope.$index,scope.row._id)"
                >修改分类</el-button
              >
              <el-button
                v-if="parentId == '0'"
                size="mini"
                @click="showSubCategory(scope.row.name,scope.row._id)"
                >查看子分类
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
      <div style="text-align: center">
        <el-pagination
          background
          @current-change="handleCurrentChange"
          :page-size="pageSize"
          layout="prev, pager, next"
          :total="total"
        >
        </el-pagination>
      </div>
    </el-card>
  </div>
</template>
<script>
import { getcategory, addcategory} from "../api/index";
import { updatecategory } from '../api/index'
export default {
  data() {
    return {
      category: {},
      list: [],
      loading: false, //加载动画
      parentId: 0,
      pageNum: 1,
      pageSize: 5,
      total: 0,
      name: "",
      categoryId: "",
      show:false
    };
  },
  async mounted() {
    let { data } = await getcategory({
      parentId: this.parentId,
      pageNum: this.pageNum,
      pageSize: this.pageSize,
    });
    this.category = data.data;
    this.list = data.data.list;
    this.total = data.data.total;
  
  },
  methods: {
    open() {
      this.$prompt("分类名称", "添加分类", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
      })
        .then(({ value }) => {
          if (!value) {
            this.$message({
              type: "error",
              message: "不能为空",
            });
            return;
          }
          this.$message({
            type: "success",
            message: "添加成功！",
          });
          this.addcategory(value);
        })
        .catch(() => {});
    },
    async getcategory(id,val) {
      let { data } = await getcategory({
        parentId: id,
        pageNum: val,
        pageSize: this.pageSize,
      });
      this.category = data.data;
      this.list = data.data.list;
      this.total=data.data.total;
    },
    
    async addcategory(name) {
      await addcategory({ parentId: this.parentId, categoryName: name });
    },
    async updatecategory(id,name) {
        await updatecategory({ categoryId: id, categoryName: name });
    },
    handleCurrentChange(val) {
      this.getcategory(this.parentId,val);
    },
    handelupdateCategory(index,id) {
      this.$prompt("输入新的分类名", "修改分类", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
      })
        .then(({ value }) => {
          if (!value) {
            this.$message({
              type: "error",
              message: "不能为空",
            });
            return;
          }
          this.$message({
            type: "success",
            message: "修改成功！",
          });
          this.list[index].name=value
          this.updatecategory(id, value);

        })
        .catch(() => {});
    },
    showSubCategory(name,id) {
        this.show=true,
        this.name=name
        this.parentId=id
        this.getcategory(this.parentId,1)
    },
    headleShow(){
      this.show=false,
      this.name=''
      this.parentId=0,
      this.getcategory(this.parentId,1)
    },
  },
};
</script>